package com.zenithmind.chat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.chat.pojo.entity.ChatPrivateSessionUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 私聊会话用户映射器 - 遵循单一职责原则
 * 只负责私聊会话用户关系的数据访问操作
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface ChatPrivateSessionUserMapper extends BaseMapper<ChatPrivateSessionUser> {

    /**
     * 根据会话ID查询用户列表
     * 
     * @param sessionId 会话ID
     * @return 用户列表
     */
    @Select("SELECT * FROM chat_private_session_user WHERE session_id = #{sessionId} AND deleted = 0")
    List<ChatPrivateSessionUser> selectBySessionId(@Param("sessionId") String sessionId);

    /**
     * 根据用户ID查询会话列表
     * 
     * @param userId 用户ID
     * @return 会话用户关系列表
     */
    @Select("SELECT * FROM chat_private_session_user WHERE user_id = #{userId} AND deleted = 0 ORDER BY update_time DESC")
    List<ChatPrivateSessionUser> selectByUserId(@Param("userId") String userId);

    /**
     * 根据会话ID和用户ID查询关系
     * 
     * @param sessionId 会话ID
     * @param userId 用户ID
     * @return 会话用户关系
     */
    @Select("SELECT * FROM chat_private_session_user WHERE session_id = #{sessionId} AND user_id = #{userId} AND deleted = 0")
    ChatPrivateSessionUser selectBySessionIdAndUserId(@Param("sessionId") String sessionId, @Param("userId") String userId);

    /**
     * 检查用户是否在会话中
     * 
     * @param sessionId 会话ID
     * @param userId 用户ID
     * @return 是否存在
     */
    @Select("SELECT COUNT(*) FROM chat_private_session_user WHERE session_id = #{sessionId} AND user_id = #{userId} AND deleted = 0")
    int checkUserInSession(@Param("sessionId") String sessionId, @Param("userId") String userId);

    /**
     * 更新最后访问时间
     * 
     * @param sessionId 会话ID
     * @param userId 用户ID
     * @param lastAccessTime 最后访问时间
     * @return 更新结果
     */
    @Update("UPDATE chat_private_session_user SET last_access_time = #{lastAccessTime}, update_time = NOW() " +
            "WHERE session_id = #{sessionId} AND user_id = #{userId} AND deleted = 0")
    int updateLastAccessTime(@Param("sessionId") String sessionId, 
                            @Param("userId") String userId, 
                            @Param("lastAccessTime") LocalDateTime lastAccessTime);

    /**
     * 更新未读消息数
     * 
     * @param sessionId 会话ID
     * @param userId 用户ID
     * @param unreadCount 未读消息数
     * @return 更新结果
     */
    @Update("UPDATE chat_private_session_user SET unread_count = #{unreadCount}, update_time = NOW() " +
            "WHERE session_id = #{sessionId} AND user_id = #{userId} AND deleted = 0")
    int updateUnreadCount(@Param("sessionId") String sessionId, 
                         @Param("userId") String userId, 
                         @Param("unreadCount") Integer unreadCount);

    /**
     * 增加未读消息数
     * 
     * @param sessionId 会话ID
     * @param userId 用户ID
     * @return 更新结果
     */
    @Update("UPDATE chat_private_session_user SET unread_count = unread_count + 1, update_time = NOW() " +
            "WHERE session_id = #{sessionId} AND user_id = #{userId} AND deleted = 0")
    int incrementUnreadCount(@Param("sessionId") String sessionId, @Param("userId") String userId);

    /**
     * 清零未读消息数
     * 
     * @param sessionId 会话ID
     * @param userId 用户ID
     * @return 更新结果
     */
    @Update("UPDATE chat_private_session_user SET unread_count = 0, last_read_time = NOW(), update_time = NOW() " +
            "WHERE session_id = #{sessionId} AND user_id = #{userId} AND deleted = 0")
    int clearUnreadCount(@Param("sessionId") String sessionId, @Param("userId") String userId);

    /**
     * 更新会话置顶状态
     * 
     * @param sessionId 会话ID
     * @param userId 用户ID
     * @param isTop 是否置顶
     * @return 更新结果
     */
    @Update("UPDATE chat_private_session_user SET is_top = #{isTop}, update_time = NOW() " +
            "WHERE session_id = #{sessionId} AND user_id = #{userId} AND deleted = 0")
    int updateTopStatus(@Param("sessionId") String sessionId, 
                       @Param("userId") String userId, 
                       @Param("isTop") Boolean isTop);

    /**
     * 更新会话免打扰状态
     * 
     * @param sessionId 会话ID
     * @param userId 用户ID
     * @param isMuted 是否免打扰
     * @return 更新结果
     */
    @Update("UPDATE chat_private_session_user SET is_muted = #{isMuted}, update_time = NOW() " +
            "WHERE session_id = #{sessionId} AND user_id = #{userId} AND deleted = 0")
    int updateMuteStatus(@Param("sessionId") String sessionId, 
                        @Param("userId") String userId, 
                        @Param("isMuted") Boolean isMuted);

    /**
     * 查询用户的置顶会话列表
     * 
     * @param userId 用户ID
     * @return 置顶会话列表
     */
    @Select("SELECT * FROM chat_private_session_user WHERE user_id = #{userId} AND is_top = 1 AND deleted = 0 ORDER BY update_time DESC")
    List<ChatPrivateSessionUser> selectTopSessionsByUserId(@Param("userId") String userId);

    /**
     * 统计用户未读消息总数
     * 
     * @param userId 用户ID
     * @return 未读消息总数
     */
    @Select("SELECT COALESCE(SUM(unread_count), 0) FROM chat_private_session_user WHERE user_id = #{userId} AND deleted = 0")
    Long countTotalUnreadByUserId(@Param("userId") String userId);
}
